define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        keyword: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'autoreply/keyword',
                    add_url: 'autoreply/add',
                    edit_url: 'autoreply/edit',
                    del_url: 'autoreply/del',
                }
            });
            var table = $("#table");
            var ids = [];
            table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, row) {
                if (e.type == 'check' || e.type == 'uncheck') {
                    row = [row];
                } else {
                    ids = [];
                }
                $.each(row, function (i, j) {
                    if (e.type.indexOf("uncheck") > -1) {
                        var index = urlArr.indexOf(j.id);
                        if (index > -1) {
                            ids.splice(index, 1);
                        }
                    } else {
                        ids.indexOf(j.id) == -1 && ids.push(j.id);
                    }
                });
                if(ids.length > 0){
                    $(".submit-synch").removeAttr('disabled')

                }else{
                    $(".submit-synch").attr('disabled',true)
                }
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'name', title: __('规则名称'),align:'left'},
                        {field: 'keyword', title: __('关键词'),align:'left',operate:false,formatter: Controller.formatter.keywords},
                        {field: 'updated_at', title: __('更新时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'type', title: __('回复内容'), formatter: Table.api.formatter.status, searchList: {text: __('回复文字'), image: __('回复图片'),material:'回复图文'}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                            return Table.api.formatter.operate.call(this, value, row, index);
                        }}
                    ]
                ]
            });
            // 同步数据
            $("body").on('click',".submit-synch",function(){
                if(ids.length > 0){
                    var url = $(this).data('href');
                    if(url.indexOf("?") != -1){
                        url += "&ids="+ids.join(",")
                    }else{
                        url += "?ids="+ids.join(",")
                    }
                    Fast.api.open(url,"同步数据");
                }
                
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            
           Controller.api._keywordInit();
        },
        edit: function () {
            Controller.api._keywordInit();
        },
        follow: function () {
            Controller.api._init();
        },
        message:function(){
            Controller.api._init();
        },
        api: {
            _init:function(){
                if($("input[name='row[category]']:checked").val() == 'image'){
                    $("#_image").show();
                    $("#_text").hide()
                }else{
                    $("#_image").hide();
                    $("#_text").show()
                }
                $("body").on("click","input[name='row[category]']",function(){
                    if($(this).val() == 'image'){
                        $("#_image").show();
                        $("#_text").hide()
                    }else{
                        $("#_image").hide();
                        $("#_text").show()
                    }
                })
                // 上传成功
                $("#faupload-image").data("upload-success", function(rest){
                    $("#_media_id").val(rest.media_id)
                });
                Form.api.bindevent($("form[role=form]"));
            },
            _keywordInit(){
                // 添加关键词
                $("body").on("click",".keyword-push",function(){
                    var temp = $("#_keywordTempHtml").html();
                    temp = temp.replace(/{_index}/,Math.ceil(Math.random()*100000));
                    $("#_keywordItem").append(temp);
                })
                // 删除关键词
                $("body").on("click",".keyword-remove",function(){
                    $(this).parent().remove();
                });
                // 选择类
                var appmsgType = 'text';
                $("body").on('click',"input[name='row[type]']",function(){
                    if($(this).val() == appmsgType ){
                        if(appmsgType != 'mpnews'){
                            Fast.api.open($(this).data('href'),'选择图文')
                        }
                        return false;
                    }
                    appmsgType = $(this).val();
                    $("._content").hide();
                    $(".content-"+appmsgType).show();
                    // 如果是图文
                    if(appmsgType == 'mpnews'){
                        Fast.api.open($(this).data('href'),'选择图文')
                    }
                })
                // 上传成功
                $("#faupload-image").data("upload-success", function(rest){
                    $("#_media_id").val(rest.media_id)
                }); 
                 Form.api.bindevent($("form[role=form]"));
            }
        },
        formatter:{
            keywords:function(value){
                return value.join(" ; ")
            }
        }
    };
    return Controller;
});
